package pl.edu.uj.tcs.blef.protocol;

import java.io.PrintWriter;
import java.net.Socket;

import pl.edu.uj.tcs.blef.server.Table;

public class SetNewGameRequestHandler extends AbstractTableHandler {

	public SetNewGameRequestHandler(Table table) {
		super(table);
	}

	@Override
	public void handle(Request request, Socket socket) {
		System.out.println("Handling " + request.message + " request from " + socket.getInetAddress().getHostAddress());
		SetNewGameRequest ctr = (SetNewGameRequest)request;
		if(!table.isOperator(socket)) {
			System.out.println("Unauthorized operator request from socket: " + socket.getInetAddress().getHostAddress());
			try {
				Request r = new UnknownRequestErrorFactory().build(null, new NullRequestHandler());
				r.send(new PrintWriter(socket.getOutputStream()));
			} catch (Exception e){}
		} else {
			try {
				String[] param = new String[1]; param[0] = "OK New game";
				Request r = new OKRequestFactory().build(param, new NullRequestHandler());
				r.send(new PrintWriter(socket.getOutputStream()));
				table.setNewGame();
			} catch (Exception e){}
		}
	}

}
